// miniprogram/pages/admin/portfolio/newPortfolio/newPortfolio.js
const MAX_IMAGE_SIZE = 1024 * 1024 * 1024;
const uploader = require('../../utils/upload.js');
const util = require('../../utils/util.js');
const app = getApp();

Page({
  removeImages: [],
  /**
   * 页面的初始数据
   */
  data: {
    settings: {},
    current: 0,
    sort: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      settings: app.globalData.settings


    })
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }

    });
  },
  changeEditMode() {
    this.setData({
      sort: this.data.sort ? false : true
    })
  },
  tapItem(e) {
    console.log(e.detail)
    this.setData({
      current: e.detail
    })
  },
  updateData(e) {
    //console.log(e.detail)
    this.setData({
      [`settings.banner`]: e.detail
    })
  },
  swiperChange(e) {
    // console.log(e.detail)
    this.setData({
      current: e.detail.current
    })
  },
  switchChange(e) {
    console.log(e.detail.value)
    let banner = this.data.settings.banner
    let currentBanner = banner[this.data.current]
    currentBanner.type = e.detail.value
    this.setData({
      [`settings.banner`]: banner
    });
    console.log(this.data.settings.banner)
  },
  inputChange(e) {

    console.log(e)
    let id = e.target.id
    let banner = this.data.settings.banner
    let currentBanner = banner[this.data.current]
    currentBanner[id] = e.detail.value
    this.setData({
      [`settings.banner`]: banner
    });
    console.log(this.data.settings.banner)
  },
  chooseBannerImg: function () {
    const that = this;
    wx.navigateTo({
      url: '/pages/admin/gallary/gallary',
      success: res => {
        res.eventChannel.on('setImages', items => {
          console.log(items)
          let banner = this.data.settings.banner;
          if (!Array.isArray(banner)) banner = [];
          items.map(v => {
            delete v.width
            delete v._id
            v.type = 'none'
            banner.push(v);
          })
          that.setData({
            ['settings.banner']: banner
          });
        });
        let banner = this.data.settings.banner;
        if (!banner) banner = [];
        res.eventChannel.emit('setImages', {
          count: 9 - banner.length,
          items: banner
        });
        // console.log(this.data.settings.banner)
      }
    })
  },
  changeBannerImg(e) {
    let index = e.currentTarget.dataset.index
    let banner = this.data.settings.banner;
    const that = this;
    wx.navigateTo({
      url: '/pages/admin/gallary/gallary',
      success: res => {
        res.eventChannel.on('setImages', items => {
          console.log(items)

          banner[index].src = items[0].src
          // if (!Array.isArray(banner)) banner = [];
          // items.map(v => {
          //   delete v.width
          //   delete v._id
          //   v.type = 'none'
          //   banner.push(v);
          // })
          that.setData({
            ['settings.banner']: banner
          });
        });
        res.eventChannel.emit('setImages', {
          count: 1,
          items: banner
        });
        // console.log(this.data.settings.banner)
      }
    })
  },
  removeBannerImg(e) {
    const that = this;
    let index = e.currentTarget.dataset.index
    let banner = this.data.settings.banner;

    banner.splice(index, 1)
    that.setData({
      'settings.banner': banner
    })

  },
  apply: function (e) {
    let settings = this.data.settings;
    this.saveSetting(settings);
  },
  updateNotice(e) {
    console.log(e)
    let settings = e.detail.value
    settings._id = this.data.settings._id
    this.saveSetting(settings);
  },
  saveSetting(settings) {
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        op: 'update',
        table: 'setting',
        data: settings
      },
      success: res => {
        wx.showToast({
          title: '数据保存成功',
        })

        let pages = getCurrentPages();
        if (res.result._id || res.result.stats.updated) {
          if (pages.length > 1) {
            pages[pages.length - 2].onLoad(pages[pages.length - 2].options);
          }
          wx.navigateBack();
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})